//
//  PaperMessageView.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/3/1.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperMessageView.h"

@interface PaperMessageView ()

@property (nonatomic, strong) UIView  *backView;
@property (nonatomic, strong) UILabel *titleLab;

@property (nonatomic, strong) RACSubject *hiddenSignal;  // 隐藏Signal

@end

@implementation PaperMessageView

- (instancetype)initWithTitle:(NSMutableAttributedString *)title {
    self = [self initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self) {
        [self setupConfigure];
        self.titleLab.attributedText = title;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupConfigure];
    }
    return self;
}

- (void)setupConfigure {
    UITapGestureRecognizer *singleFingerTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    [self addGestureRecognizer:singleFingerTap];
    
    self.hiddenSignal = [RACSubject subject];
    
    [self addSubview:self.backView];
    [self addSubview:self.titleLab];
}

#pragma makr - gesture

//The event handling method
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
    self.hidden = YES;
    [self.hiddenSignal sendNext:[NSDate date]];
}

#pragma mark - getter

- (UIView *)backView {
    if (_backView == nil) {
        _backView = [[UIView alloc] initWithFrame:self.frame];
        _backView.backgroundColor = RGBA(0, 0, 0, 0.4);
    }
    return _backView;
}

- (UILabel *)titleLab {
    if (_titleLab == nil) {
        _titleLab = [[UILabel alloc] initWithFrame:self.frame];
        _titleLab.textColor = [UIColor whiteColor];
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.numberOfLines = 0;
    }
    return _titleLab;
}

#pragma mark - ViewSignalProtocol

- (RACSignal *)getSignalWithDictionary:(NSDictionary *)dict {
    NSString *str = dict[@"action"];
    if (!str)   return [RACSignal empty];
    
    if ([str isEqualToString:@"hidden"]) {     // 画板
        return self.hiddenSignal;
    } else {
        return [RACSignal empty];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
